﻿namespace Microsoft.VisualStudio.TextTemplating.CommandLine
{
    using System;
    using System.Collections;
    using System.Text;

    internal sealed class FormatUtility
    {
        private FormatUtility()
        {
        }

        public static string FormatStringForWidth(string toFormat, int offset, int hangingIndent, int width)
        {
            string[] strArray = MakeWords(toFormat);
            StringBuilder builder = new StringBuilder();
            string str = new string(' ', offset + hangingIndent);
            int num = 0;
            int index = 0;
            int num3 = 0;
            while (index < strArray.Length)
            {
                num3 = index;
                int num4 = offset;
                if (num > 0)
                {
                    num4 += hangingIndent;
                }
                num4 += strArray[index].Length;
                if (strArray[index].EndsWith(".", StringComparison.OrdinalIgnoreCase))
                {
                    num4 += 2;
                }
                else
                {
                    num4++;
                }
                while (((num3 + 1) < strArray.Length) && ((num4 + strArray[num3 + 1].Length) < width))
                {
                    num3++;
                    num4 += strArray[num3].Length;
                    if (strArray[num3].EndsWith(".", StringComparison.OrdinalIgnoreCase))
                    {
                        num4 += 2;
                    }
                    else
                    {
                        num4++;
                    }
                }
                if (num > 0)
                {
                    builder.Append(str);
                }
                else
                {
                    builder.Append(new string(' ', offset));
                }
                for (int i = index; i <= num3; i++)
                {
                    if (i > index)
                    {
                        if (strArray[i - 1].EndsWith(".", StringComparison.OrdinalIgnoreCase))
                        {
                            builder.Append("  ");
                        }
                        else
                        {
                            builder.Append(" ");
                        }
                    }
                    builder.Append(strArray[i]);
                }
                builder.Append(Environment.NewLine);
                num++;
                index = num3 + 1;
            }
            return builder.ToString();
        }

        public static string[] MakeWords(string toParse)
        {
            char[] chArray = toParse.ToCharArray();
            StringBuilder builder = new StringBuilder();
            ArrayList list = new ArrayList();
            for (int i = 0; i < chArray.Length; i++)
            {
                if (char.IsWhiteSpace(chArray[i]))
                {
                    if (builder.Length > 0)
                    {
                        list.Add(builder.ToString());
                        builder.Length = 0;
                    }
                }
                else
                {
                    builder.Append(chArray[i]);
                }
            }
            if (builder.Length > 0)
            {
                list.Add(builder.ToString());
            }
            return (string[]) list.ToArray(typeof(string));
        }

        private enum ParseState
        {
            Start,
            StartOfLine,
            ReadNext,
            EndOfLine,
            EndOfString
        }
    }
}

